<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入班级名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="班主任ID" prop="teacher">-->
<!--        <el-input-->
<!--          v-model="queryParams.teacher"-->
<!--          placeholder="请输入班主任ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="辅导员ID" prop="teacher2">-->
<!--        <el-input-->
<!--          v-model="queryParams.teacher2"-->
<!--          placeholder="请输入辅导员ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="班级类型;0 原始班级  1 行政班级" prop="category">-->
<!--        <el-input-->
<!--          v-model="queryParams.category"-->
<!--          placeholder="请输入班级类型;0 原始班级  1 行政班级"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teach:clazz:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teach:clazz:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teach:clazz:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teach:clazz:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clazzList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="班级ID" align="center" prop="cId" />
      <el-table-column label="班级名称" align="center" prop="name" />
      <el-table-column label="班主任" align="center" prop="teacherName" />
      <el-table-column label="辅导员" align="center" prop="teacher2Name" />
      <el-table-column label="班级类型" align="center" prop="category" >
        <template slot-scope="scope">
          {{scope.row.category == 0 ? "行政班级":"教学班级"}}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleTeacher(0)"-->
<!--            v-hasPermi="['teach:clazz:edit']"-->
<!--          >指定班主任</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleTeacher(1)"-->
<!--            v-hasPermi="['teach:clazz:edit']"-->
<!--          >指定辅导员</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleStudent(scope.row)"
            v-hasPermi="['teach:clazz:edit']"
          >查看班级学生</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teach:clazz:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teach:clazz:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改班级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班级名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入班级名称" />
        </el-form-item>
        <el-form-item label="班主任" prop="teacher">
<!--          <el-input v-model="form.teacher" placeholder="请输入班主任ID" />-->
          <el-select v-model="form.teacher">
            <el-option :value="0" label="请选择" ></el-option>
            <el-option v-for="item in teacherList"
                       :key="item.userId"
                       :value="item.userId"
                       :label="item.userName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="辅导员" prop="teacher2">
<!--          <el-input v-model="form.teacher2" placeholder="请输入辅导员ID" />-->
          <el-select v-model="form.teacher2">
            <el-option :value="0" label="请选择" ></el-option>
            <el-option v-for="item in teacherList"
                       :key="item.userId"
                       :value="item.userId"
                       :label="item.userName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级类型;0 原始班级  1 行政班级" prop="category">
          <el-input v-model="form.category" placeholder="请输入班级类型;0 原始班级  1 行政班级" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="查看班级学生" :visible.sync="studentOpen"  width="800px" append-to-body>
      <el-table
        v-loading="loading"
        :data="studentList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <!--      <el-table-column label="学生" align="center" prop="sId" />-->
        <!--      <el-table-column label="学校" align="center" prop="schId" />-->
        <el-table-column label="学生姓名" align="center" prop="name" />
        <el-table-column label="学制" align="center" prop="sysName" />
        <!--      <el-table-column label="院系" align="center" prop="deptId" />-->
        <!--      <el-table-column label="专业" align="center" prop="majorId" />-->
        <el-table-column
          label="入学时间"
          align="center"
          prop="entrance"
          width="100"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.entrance, "{y}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="年龄" width="50" align="center" prop="age" />
        <el-table-column label="性别" align="center" prop="gender">
          <template slot-scope="scope">
            <span>{{ scope.row.gender == 1 ? "男" : "女" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系电话" align="center" prop="phone" />
        <el-table-column label="身份证号" align="center" prop="card" />

        <el-table-column label="户籍地址" align="center" prop="address1" />
        <el-table-column label="现居地址" align="center" prop="address2" />

        <el-table-column label="学历状态" align="center" prop="education">
          <template slot-scope="scope">
          <span v-if="scope.row.education != null">
            {{ scope.row.education == 1 ? "单招" : "成考" }}
          </span>
            <span v-else> 暂未选择 </span>
          </template>
        </el-table-column>
        <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="90"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                style="margin-left: 10px"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleClazzOut(scope.row)"
              >退班</el-button>
            </template>

        </el-table-column>

      </el-table>

    </el-dialog>

<!--    <el-dialog :title="'请选择老师'" :visible.sync="teacherOpen" width="500px" append-to-body>-->
<!--      <el-select v-model="teacherId">-->
<!--        <el-option :value="0" label="请选择" ></el-option>-->
<!--        <el-option v-for="item in teacherList"-->
<!--                   :key="item.userId"-->
<!--                   :value="item.userId"-->
<!--                   :label="item.userName"></el-option>-->
<!--      </el-select>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitTeacher">确 定</el-button>-->
<!--        <el-button @click="teacherOpen = false">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
import { listClazz, getClazz, delClazz, addClazz, updateClazz } from "@/api/teach/clazz";
import { listUser } from "@/api/system/user";
import {
  listStudent, updateStudent
} from "@/api/teach/student";

export default {
  name: "Clazz",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班级表格数据
      clazzList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        teacher: null,
        teacher2: null,
        category: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      studentList:[],
      studentOpen: false,
      // teacherOpen: false,
      // teacherId: 0,
      // actionType: 0,
      teacherList: [],
      selectClazz:{}
      // clazzId: 0
    };
  },
  created() {
    this.getList();
    this.getTeacher();
  },
  methods: {
    /** 查询班级列表 */
    getList() {
      this.loading = true;
      listClazz(this.queryParams).then(response => {
        this.clazzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTeacher(){
      this.loading = true;
      listUser({pageSize:10000,pageNum: 1}).then(response => {
        this.teacherList = response.rows;
        // this.teacherOpen = true;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cId: null,
        name: null,
        teacher: null,
        teacher2: null,
        category: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班级";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cId = row.cId || this.ids
      getClazz(cId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改班级";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cId != null) {
            updateClazz(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClazz(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cIds = row.cId || this.ids;
      this.$modal.confirm('是否确认删除班级编号为"' + cIds + '"的数据项？').then(function() {
        return delClazz(cIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teach/clazz/export', {
        ...this.queryParams
      }, `clazz_${new Date().getTime()}.xlsx`)
    },
    handleStudent(row){
      this.selectClazz = row;
      let d = {pageSize: 1000,pageNum: 1};
      if(row.category == 0){
        d.clazzId = row.cId
      }else{
        d.clazzId2 = row.cId
      }
      listStudent(d).then(response => {
        this.studentList = response.rows;
        this.studentOpen = true;

      })
    },
    handleClazzOut(row){
      let d = {sId:row.sId};
      if(this.selectClazz.category == 0){
        // 行政班级退班
        d.clazzId = 0;
      }else{
        // 教学班级退班
        d.clazzId2 = 0;
      }
      updateStudent(d).then(response => {
        this.handleStudent(this.selectClazz);
        this.$modal.msgSuccess("操作成功");
      })
    }
    // handleTeacher(row,t){
    //   this.clazzId = row.cId;
    //   this.actionType = t
    //   listUser({pageSize:10000,pageNum: 1}).then(response => {
    //     this.teacherList = response.rows;
    //     this.teacherOpen = true;
    //   });
    // },
    // submitTeacher(){
    //   let d = {cId:this.clazzId}
    //   if(this.actionType == 0 ){
    //     // 班主任
    //     d.teacher = this.teacherId;
    //   }else{
    //     // 辅导员
    //     d.teacher2 = this.teacherId;
    //   }
    // }
  }
};
</script>
